var page = 1;
var pageOld = 1;
var pageNum = 10;
var pageI = 1;
var pageL = 5;

var systemPurseWaterFuntion = function () {
    $.ajax({
        url:"usersWeb/findAllMoneyLogByUserIdPage.json",
        type:'POST',
        dataType:"json",
        data:{
            userid:loginUserId,page:page,pageNum:pageNum
        },
        success:function(result){
            var tem = "<tr>" +
                "<th width='5%'>编号</th>" +
                "<th width='20%'>类型</th>" +
                "<th width='20%'>金额</th>" +
                "<th width='55%'>时间</th>" +
                "</tr>";
            $.each( result.byMoneyLogList, function(index, content){
                tem += "<tr>" +
                    "<th width='5%'>"+content.id+"</th>";
                if(content.moneyType == '1'){
                    tem +="<th width='20%'>订单支付</th>";
                }else if(content.moneyType == '2'){
                    tem +="<th width='20%'>红包奖励</th>";
                }else if(content.moneyType == '3'){
                    tem +="<th width='20%'>冻结金额</th>";
                }else if(content.moneyType == '4'){
                    tem +="<th width='20%'>提现</th>";
                }else {
                    tem +="<th width='20%'>未知状态</th>";
                }
                tem +="<th width='20%'>"+content.amount+"</th>" +
                    "<th width='55%'>"+renderDateStr(content.createTime)+"</th>"+
                    "</tr>";
            });
            $("#systemPurseWaterTable").empty();
            $("#systemPurseWaterTable").append(tem);

            var pageSum = result.pageSum;
            var numSum = result.numSum;
            var temPage ="<a href='#' onclick='pageFunction("+page+","+page+",\"em\","+pageSum+");'><em>&lt;&lt;</em></a>";

            if(pageSum<=5){
                pageI = 1;
                pageL = pageSum;
            }else if(pageSum>5 && pageSum-pageI < 4 || pageSum-page < 4){
                pageI = pageSum-4;
                pageL = pageSum;
            }else {
                if(page >= 5){
                    if(page > pageOld){
                        if(page-pageI == 4){
                            pageI = page;
                            pageL = page+4;
                        }
                    }else if(page < pageOld){
                        if(pageL-page == 4){
                            pageI = page-4;
                            pageL = page;
                        }
                    }
                }else {
                    pageI = 1;
                    pageL = pageI+4;
                }
            }

            for(var i=pageI;i<=pageL;i++){
                if(page == i){
                    temPage += "<a class='cur' href='#' onclick='pageFunction("+i+","+page+",\"onc\");'>"+i+"</a>";
                }else {
                    temPage += "<a href='#' onclick='pageFunction("+i+","+page+",\"onc\");'>"+i+"</a>";
                }
            }

            temPage += "<a href='#' onclick='pageFunction("+page+","+page+",\"ep\","+pageSum+");'><em>&gt;&gt;</em></a>" +
                "<span>共"+numSum+"条/"+pageSum+"页</span>" +
                "<input class='pageTxt' name='pageValue' type='text' value='' />" +
                "<input class='pageBut' type='button' value='GO' onclick='pageFunction(0,"+page+",\"select\");'/>";

            $("#systemPurseWaterPage").empty();
            $("#systemPurseWaterPage").append(temPage);

            //动态加class
            $(".page a,.orderTit a").click(function(){
                $(this).addClass("cur").siblings().removeClass("cur");
            });

        }
    });
};
systemPurseWaterFuntion();

var pageFunction = function(p,o,t,s){
    if(t == 'select'){
        if(parseInt($("input[name='pageValue']").val()) < 1){
            page = 1;
        }else {
            page = parseInt($("input[name='pageValue']").val());
        }
        pageI = page - 2;
        pageL = page + 2;
    }else if(t == 'em'){
        if(p == 1){
            page = p;
        }else {
            page = p - 1;
        }
        if(p==5 || pageL-p == 4){
            pageI = p-4;
            pageL = p;
        }
    }else if(t == 'ep'){
        if(p == s){
            page = p;
        }else {
            page = p + 1;
        }
        if(p==5 || p-pageI == 4){
            pageI = p;
            pageL = p+4;
        }
    }else {
        page = p ;
    }
    systemPurseWaterFuntion();
    pageOld = o;
};